package com.chen.service.user;

import com.chen.dao.BaseDao;
import com.chen.dao.user.UserDao;
import com.chen.dao.user.UserDaoImp;
import com.chen.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImp implements UserService{
    private UserDao userDao;
    public UserServiceImp(){
        userDao = new UserDaoImp();
    }
    public User login(String userCode,String password){
        Connection connection=null;
        User user=null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }
    public boolean updatePwd(int id,String password) {
        Connection con=null;
        boolean updateFlag=false;
        try {
            con=BaseDao.getConnection();
            int updatecount=userDao.updatePwd(con,id,password);
            if (updatecount>0){
                updateFlag=true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(con,null,null);
        }
        return updateFlag;
    }
    public int getUserCount(String userName,int userRole){
        Connection con = null;
        int count = 0;
        try {
            con = BaseDao.getConnection();
            count = userDao.getUserCount(con, userName, userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(con,null,null);
        }
        return count;
    }
    @Test
    public void test(){
        UserServiceImp userServiceImp = new UserServiceImp();
        List<User> userList = userServiceImp.getUserList("孙兴",3,1,1);
        System.out.println(userList.toString());
    }
    public List<User> getUserList(String queryUserName,int queryUserRole,int currentpageNo,int PageSize){
        Connection con=null;
        List<User> userList=null;
        try {
            con = BaseDao.getConnection();
            userList = userDao.getUserList(con, queryUserName, queryUserRole, currentpageNo, PageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(con,null,null);
        }
        return userList;
    }

}
